package com.lwj.service.impl;

import com.lwj.dao.ProjectMapper;
import com.lwj.dao.ProjectTeamMapper;
import com.lwj.dao.TeamMapper;
import com.lwj.entity.Project;
import com.lwj.entity.ProjectTeam;
import com.lwj.entity.Team;
import com.lwj.service.ProjectTeamService;
import com.lwj.util.DataRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linwj
 * @date 2018/5/12 10:01
 */
@Service
@Transactional
public class ProjectTeamServiceImpl implements ProjectTeamService {

    @Autowired
    private ProjectTeamMapper projectTeamMapper;

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public DataRet<String> projectCompete(int teamId, int projectId) {
        ProjectTeam projectTeam=new ProjectTeam();
        projectTeam.setTeamId(teamId);
        projectTeam.setProjectId(projectId);
        projectTeam.setStatus("0");
        projectTeamMapper.insert(projectTeam);
        return new DataRet<>("操作成功");
    }

    @Override
    public DataRet<String> projectValidate(int teamId, int projectId) {
        ProjectTeam projectTeam=new ProjectTeam();
        projectTeam.setTeamId(teamId);
        projectTeam.setProjectId(projectId);
        if (projectTeamMapper.selectByProjectTeam(projectTeam).size()>0){
            projectTeam=projectTeamMapper.selectByProjectTeam(projectTeam).get(0);
        }
        if (projectTeam != null&& "0".equals(projectTeam.getStatus())) {
            return new DataRet<>("true");
        }else {
            return new DataRet<>("false");
        }
    }

    @Override
    public DataRet<List<Team>> getCompeteTeam(int projectId) {
        return new DataRet<>(teamMapper.getCompeteTeam(projectId));
    }

    @Override
    public DataRet<String> ensureTeam(int projectId, int teamId) {
        ProjectTeam projectTeam=new ProjectTeam();
        projectTeam.setProjectId(projectId);
        List<ProjectTeam> projectTeams=projectTeamMapper.selectByProjectTeam(projectTeam);
        for (ProjectTeam p :
                projectTeams) {
            if (p.getTeamId()==teamId){
                p.setStatus("1");
                projectTeamMapper.updateByPrimaryKey(p);
            }else {
                p.setStatus("4");
                projectTeamMapper.updateByPrimaryKey(p);
            }

        }
        Project project=new Project();
        project=projectMapper.selectByPrimaryKey(projectId);
        project.setStatus("1");
        projectMapper.updateByPrimaryKey(project);
        return new DataRet<>("操作成功");
    }
}
